package com.carbon.trade.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.api.Paging;
import com.carbon.domain.common.ApiResult;
import com.carbon.trade.common.BaseController;
import com.carbon.trade.common.CarbonCreditFeignClient;
import com.carbon.trade.entity.CarbonTradeQuote;
import com.carbon.trade.param.CarbonTradeQuoteQueryParam;
import com.carbon.trade.param.StartTradingParam;
import com.carbon.trade.service.CarbonTradeQuoteService;
import com.carbon.trade.vo.CarbonTradeQuoteQueryVo;
import com.carbon.trade.vo.CarbonTradeQuoteReturnVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.DocValueFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 碳交易供需行情 前端控制器
 * </p>
 *
 * @author lin rizhao
 * @since 2022-05-21
 */
@Slf4j
@RestController
@RequestMapping("/carbonTradeQuote")
@Api(value = "碳交易供需行情模块", tags = {"碳交易供需行情模块"})
public class CarbonTradeQuoteController extends BaseController {
    @Autowired
    private CarbonCreditFeignClient carbonCreditFeignClient;
    @Autowired
    private CarbonTradeQuoteService carbonTradeQuoteService;


    @GetMapping("/getDeliveryMethod/{type}")
    @ApiOperation(value = "获取所有期望",notes = "获取所有期望")
    public ApiResult<ArrayList> getDeliveryMethod(@PathVariable String type){
        ArrayList arrayList = carbonTradeQuoteService.getDeliveryMethod(type);
        return ApiResult.result(arrayList.size()>0,arrayList);
    }
    @GetMapping("/getSellList")
    @ApiOperation(value = "获取所有供应条目",notes = "获取所有供应条目")
    public ApiResult<ArrayList> getSellList(){
        CarbonTradeQuoteQueryVo queryVo = new CarbonTradeQuoteQueryVo();

        ArrayList<CarbonTradeQuote> list =(ArrayList) carbonTradeQuoteService.list();

        queryVo.setTotal((long) list.size());
        return ApiResult.ok(list);
    }

    /**
     * 添加碳信用
     */
    @PostMapping("/addCredit")
    public ApiResult addCredit(@RequestBody CarbonTradeQuote carbonTradeQuote ,@RequestParam Long creditId) {
        carbonTradeQuote.setAssetType("0140000001");
        carbonTradeQuote.setStatus("0160000001");
        carbonTradeQuote.setTradeRole("0270000002");
        BigDecimal total = carbonCreditFeignClient.queryTotal(creditId).getData();
        if ((total.compareTo(carbonTradeQuote.getTradeQuantity()))!=-1) {
            carbonCreditFeignClient.updateAmount(carbonTradeQuote);
            boolean row = carbonTradeQuoteService.save(carbonTradeQuote);
            return ApiResult.result(row);
        }else {
            return ApiResult.fail("持仓量不足");
        }


    }

    /**
     * 添加碳资产
     */
    @PostMapping("/addQuota")
    public ApiResult addQuota(@RequestBody CarbonTradeQuote carbonTradeQuote) {
        carbonTradeQuote.setAssetType("0140000002");
        carbonTradeQuote.setStatus("0160000001");
        carbonTradeQuote.setTradeRole("0270000002");

        BigDecimal total = carbonCreditFeignClient.queryTotalQuota(carbonTradeQuote.getProjectId()).getData();
        if ((total.compareTo(carbonTradeQuote.getTradeQuantity()))!=-1) {
            carbonCreditFeignClient.updateAmountQuota(carbonTradeQuote);
            boolean row = carbonTradeQuoteService.save(carbonTradeQuote);
            return ApiResult.result(row);
        }else {
            return ApiResult.fail("持仓量不足");
        }


    }
    @PostMapping("/handleSearch")
    public ApiResult<CarbonTradeQuoteReturnVo> handleSearch(@RequestBody CarbonTradeQuoteQueryParam param) {
        param.setTradeRole("0270000002");
        param.setAssetType(carbonTradeQuoteService.transferType(param.getAssetType()));
        param.setProjectType(carbonTradeQuoteService.transferType(param.getProjectType()));
        Page<CarbonTradeQuote> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<CarbonTradeQuote> paging = carbonTradeQuoteService.handleSearch(page,param);
        CarbonTradeQuoteReturnVo carbonTradeQuoteReturnVo = new CarbonTradeQuoteReturnVo();
        carbonTradeQuoteReturnVo.setTotal((int) paging.getTotal());
        for (int i = 0; i < paging.getRecords().size(); i++) {
            paging.getRecords().get(i).setAssetType(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getAssetType()));
            paging.getRecords().get(i).setProjectType(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getProjectType()));
            paging.getRecords().get(i).setDeliveryExchange(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getDeliveryExchange()));
            paging.getRecords().get(i).setDeliveryMethod(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getDeliveryMethod()));

        }
        carbonTradeQuoteReturnVo.setList(paging.getRecords());
        return ApiResult.ok(carbonTradeQuoteReturnVo);
    }
    @PostMapping("/handleSearchB")
    public ApiResult<CarbonTradeQuoteReturnVo> handleSearchB(@RequestBody CarbonTradeQuoteQueryParam param) {
        param.setTradeRole("0270000001");
        param.setAssetType(carbonTradeQuoteService.transferType(param.getAssetType()));
        param.setProjectType(carbonTradeQuoteService.transferType(param.getProjectType()));
        Page<CarbonTradeQuote> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<CarbonTradeQuote> paging = carbonTradeQuoteService.handleSearch(page,param);
        CarbonTradeQuoteReturnVo carbonTradeQuoteReturnVo = new CarbonTradeQuoteReturnVo();
        carbonTradeQuoteReturnVo.setTotal((int) paging.getTotal());
        for (int i = 0; i < paging.getRecords().size(); i++) {
            paging.getRecords().get(i).setAssetType(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getAssetType()));
            paging.getRecords().get(i).setProjectType(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getProjectType()));
            paging.getRecords().get(i).setDeliveryExchange(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getDeliveryExchange()));
            paging.getRecords().get(i).setDeliveryMethod(carbonTradeQuoteService.transferNumToChinese(paging.getRecords().get(i).getDeliveryMethod()));

        }
        carbonTradeQuoteReturnVo.setList(paging.getRecords());
        return ApiResult.ok(carbonTradeQuoteReturnVo);
    }


    @PostMapping("/addBuy")
    public ApiResult addBuy(@RequestBody CarbonTradeQuote carbonTradeQuote) {
        //交易状态
        carbonTradeQuote.setStatus("0160000001");
        carbonTradeQuote.setTradeRole("0270000001");



        carbonTradeQuote.getProjectScopeCode();

        boolean row = carbonTradeQuoteService.save(carbonTradeQuote);
        return ApiResult.result(row);
    }



}

